package com.elective.courseselect.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elective.courseselect.dao.QuestionDao;
import com.elective.courseselect.entity.QuestionEntity;
import com.elective.courseselect.entity.QuestionnaireEntity;
import com.elective.courseselect.enums.QuestionnaireType;
import com.elective.courseselect.service.QuestionService;
import com.elective.courseselect.service.QuestionnaireService;
import com.elective.courseselect.utils.PageUtils;
import com.elective.courseselect.utils.Query;
import com.elective.courseselect.utils.R;
import com.elective.status.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;



@Service("questionService")
@Transactional(rollbackFor = Exception.class)
public class QuestionServiceImpl extends ServiceImpl<QuestionDao, QuestionEntity> implements QuestionService {

    @Autowired
    QuestionnaireService questionnaireService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<QuestionEntity> page = this.page(
                new Query<QuestionEntity>().getPage(params),
                new QueryWrapper<QuestionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R searchCharacterQuestion(Integer questionnaireId) {
        QueryWrapper<QuestionnaireEntity> questionnaireEntityQueryWrapper = new QueryWrapper<>();
        questionnaireEntityQueryWrapper.eq("id", questionnaireId);
        QuestionnaireEntity questionnaireEntity = questionnaireService.getOne(questionnaireEntityQueryWrapper);
        if (questionnaireEntity.getType() != QuestionnaireType.PERSONALITY_QUESTIONNAIRE) {
            return R.ok(StatusCode.ERROR,questionnaireId + "此问卷不属于性格调查问卷");
        }



        QueryWrapper<QuestionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("questionnaire_id", questionnaireId);
        List<QuestionEntity> list = this.list(wrapper);
        for (QuestionEntity questionEntity : list) {
            questionEntity.setOptionByJson(JSONObject.parseArray(questionEntity.getOption()));
            questionEntity.setReferenceByJson(JSONObject.parseArray(questionEntity.getReference()));
        }
        return R.ok(StatusCode.OK,questionnaireId + "查询性格调查问卷查询成功").put("list",list);
    }

    @Override
    public void updateTestPaper(List<QuestionEntity> questionEntity) {
        for (QuestionEntity entity : questionEntity) {
            // 新增
            if (0 == entity.getOperation()) {
                entity.setId(null);
                entity.setOption(entity.getOptionByJson().toJSONString());
                entity.setReference(entity.getReferenceByJson().toJSONString());
                this.save(entity);
            }
            // 修改
            if (1 == entity.getOperation()) {
                entity.setOption(entity.getOptionByJson().toJSONString());
                entity.setReference(entity.getReferenceByJson().toJSONString());
                this.updateById(entity);
            }
            // 删除
            if (2 == entity.getOperation()) {
                this.removeById(entity.getId());
            }
        }
    }

    /**
     * Application构建查询对象
     * @param questionEntity
     * @return
     */
    public static QueryWrapper createExample(QuestionEntity questionEntity){
        QueryWrapper<QuestionEntity> wrapper = new QueryWrapper<>();
        if(questionEntity!=null){
            //
            if(!StringUtils.isEmpty(questionEntity.getId())){
                wrapper.eq("id",questionEntity.getId());
            }
            //
            if(!StringUtils.isEmpty(questionEntity.getQuestionnaireId())){
                wrapper.eq("questionnaire_id",questionEntity.getQuestionnaireId());
            }
            if(!StringUtils.isEmpty(questionEntity.getType())){
                wrapper.eq("type",questionEntity.getType());
            }
            if(!StringUtils.isEmpty(questionEntity.getType())){
                wrapper.eq("value",questionEntity.getValue());
            }
            if(!StringUtils.isEmpty(questionEntity.getType())){
                wrapper.eq("option",questionEntity.getOption());
            }
            if(!StringUtils.isEmpty(questionEntity.getType())){
                wrapper.eq("reference",questionEntity.getReference());
            }
            if(!StringUtils.isEmpty(questionEntity.getType())){
                wrapper.eq("characters_id",questionEntity.getCharactersId());
            }
        }
        return wrapper;
    }




}